// Axios网路请求相关配置
import axios from 'axios'
// import vue from "vue"
import {getUserToken} from '@/utils/token'
import {devConfig} from '@/http/config'


let instance = axios.create({
	baseURL: devConfig.baseURL,
	timeout: devConfig.timeout,
	withCredentials: devConfig.withCredentials,
	headers: devConfig.headers
})

/**
 * 请求:所有请求都需要带上请求头
 * 除了登录
 */
instance.interceptors.request.use(
	config => {
		// login直接放行
		if (config.url === 'login') {
			return config
		} else {
			config.headers['Authorization'] = getUserToken() // 请求头带上token
			return config
		}
	},
	error => {
		return Promise.reject('no token' + error)
	})

/**
 * 响应:当响应状态码为401时,token失效
 */
instance.interceptors.response.use(
	response => {
		if (response.data && response.data.code === 401) { // 401, token失效
			// token失效
			//clearLoginInfo()
			// 跳转到login界面
			//router.push({ name: 'login' })
		}
		return response
	},
	error => {
		return Promise.reject(error)
	})


export default instance
